package com.gitee.wsl.transform.escape


import com.gitee.wsl.text.replacer.AbstractStrReplacer
import com.gitee.wsl.text.replacer.QuickReplacer


//object Escape

fun interface Warp<T>{
    fun escape(base:T):T
}

fun interface UnWarp<T>{
    fun unEscape(base:T):T
}

 interface Escape<T>: Warp<T>, UnWarp<T>{
     companion object
 }

open class EscapeString: Escape<String> {

    open val warpEntrys:List<Pair<String,String>> = emptyList()

    open val strReplacer: AbstractStrReplacer by lazy { QuickReplacer(warpEntrys) }

    open val strUnWarpReplacer:AbstractStrReplacer by lazy { QuickReplacer(warpEntrys.map { entry -> entry.second to entry.first }) }

    override fun escape(base: String): String {
        return strReplacer.replace(base)
    }

    override fun unEscape(base: String): String {
        return strUnWarpReplacer.replace(base)
    }
}


fun String.escape(vararg warp: Escape<String>):String {
    var base = this
    warp.forEach {
        base = it.escape(base)
    }
    return base
}

fun String.unescape(vararg warp: Escape<String>):String {
    var base = this
    warp.forEach {
        base = it.unEscape(base)
    }
    return base
}
